var myUtils = {
    // -> 获取另一个页面传入的参数
    GetRequest: function () {
        var url = location.search; //获取url中"?"符后的字串  
        var theRequest = new Object();

        url = decodeURI(url)
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        // console.log("GetRequest: ", theRequest)
        return theRequest;
    },
    // -> 获取今天的日期: @returns 2021-04-01
    getToday: function () {
        var nowDate = new Date();
        var year = nowDate.getFullYear();
        var month =
            nowDate.getMonth() + 1 < 10
                ? "0" + (nowDate.getMonth() + 1)
                : nowDate.getMonth() + 1;
        var day =
            nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
        var dateStr = year + "-" + month + "-" + day;
        // console.log("dateStr:", dateStr)
        return dateStr;
    },
    // -> 把JSON格式的字符串转换为JSON格式的对象
    jsonParse: function (str) {
        return "JSON" in window ? JSON.parse(str) : eval("(" + str + "+");
    },
    /**
     * 当前日期+天数=目标日期
     */
    dayAdd: function (date, days) {
        var d = new Date(date);
        d.setDate(d.getDate() + days);
        var m = d.getMonth() + 1;
        var month = m < 10 ? "0" + m : m;
        var day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
        return d.getFullYear() + '-' + month + '-' + day;
    },
    /**
     * 拼接地址 to array json
     */
    pieceAddress: function (params, size) {
        var address = new Array();
        for (var i = 1; i < size + 1; i++) {
            let province = params['province-' + i]
            let city = params['city-' + i]
            let area = params['area-' + i]
            if (province == '全部') {
                province = null
            } else if (city == '全部') {
                city = null
            } else if (area == '全部') {
                area = null
            }
            address.push({
                province: province,
                city: city,
                area: area
            })
            delete params['province-' + i]
            delete params['city-' + i]
            delete params['area-' + i]
        }
        // console.log("address: ", address)
        params["involvedRegion"] = address;
        return params
    },
    /**
     * 拼接举报人信息 to array json
     */
    pieceReporter: function (params) {
        var reporter = new Object();
        reporter['name'] = params['name']
        reporter['cardId'] = params['cardId']
        reporter['phone'] = params['phone']
        reporter['location'] = params['location']
        reporter['memo'] = params['memo']

        delete params['name']
        delete params['cardId']
        delete params['phone']
        delete params['location']
        delete params['memo']
        params["reporter"] = reporter;
        return params
    },
    /**
     * 拼接被举报人信息 to array json
     */
    pieceBlackInfo: function (params, blackSelected) {
        var blackInfo = new Array();
        let len = Object.getOwnPropertyNames(blackSelected).length
        // console.log("len: ", len)
        // console.log("blackSelected: ", blackSelected)
        for (let i = 1; i <= len; i++) {
            let caseType = blackSelected['blackCascader-' + i][1]
            var caseContentOne, caseContentTwo
            if (caseType == 0) {
                caseContentOne = blackSelected['blackCascader-' + i][2]
                caseContentTwo = -1
            } else if (caseType == 1) {
                caseContentOne = -1
                caseContentTwo = blackSelected['blackCascader-' + i][2]
            } else if (caseType == 2) {
                caseContentOne = -1
                caseContentTwo = -1
            }
            blackInfo.push({
                name: params['blackName-' + i],
                blackCardId: params['blackCardId-' + i],
                isPolice: blackSelected['blackCascader-' + i][0],
                caseType: caseType,
                caseContentOne: caseContentOne,
                caseContentTwo: caseContentTwo,
                blackDepartment: blackSelected['blackCascader-' + i][3],
                title: blackSelected['blackCascader-' + i][4],
            })
            delete params['blackName-' + i]
            delete params['blackCardId-' + i]
        }
        // console.log("blackInfo: ", blackInfo)
        params["reporteders"] = blackInfo;
        return params
    },
    /**
     * 清洗xm-select插件的数据
     */
    cleanXmSelect: function (dataObj) {
        var xmSelectRes = new Array();
        for (let i = 0; i < dataObj.length; i++) {
            // xmSelectRes.push({
            //     value: dataObj[i].value,
            //     name: dataObj[i].name
            // })
            xmSelectRes.push(dataObj[i].value)
        }
        // console.log("xmSelectRes: ", xmSelectRes)
        return xmSelectRes
    },
    /**
     * 将数组|对象的空字符串转为Null
     */
    setNull: function (dataObj) {
        // if (Array.isArray(dataObj)) {
        //     for (let index = 0; index < dataObj.length; index++) {
        //         let element = dataObj[index];
        //         for (let i in element) {
        //             if (element[i] == undefined) {
        //                 element[i] = null
        //             }
        //         }
        //         dataObj[index] = element
        //     }
        // } else {
        //     for (let i in dataObj) {
        //         if (dataObj[i] == undefined) {
        //             dataObj[i] = null
        //         }
        //     }
        // }
        if (dataObj.sendTime == '') {
            dataObj.sendTime = null
        }
        if (dataObj.feedbackTime == '') {
            dataObj.feedbackTime = null
        }
        return dataObj
    },
    /**
     * 将数组|对象的Null转为空字符串
     */
    nullTransStr: function (dataObj) {
        // if (Array.isArray(dataObj)) {
        //     for (let index = 0; index < dataObj.length; index++) {
        //         let element = dataObj[index];
        //         for (let i in element) {
        //             if (element[i] == null || element[i] == undefined) {
        //                 element[i] = ''
        //             }
        //         }
        //         dataObj[index] = element
        //     }
        // } else {
        for (let i in dataObj) {
            if (dataObj[i] == null || dataObj[i] == undefined) {
                dataObj[i] = ''
            }
        }
        // }
        return dataObj
    },
    /**
     * 把未选择的option重置为-1
     */
    initOption: function (dataObj) {
        var selects = ['reportType', 'reportFrom', 'reportState', 'moreLetter', 'isRepeat', 'isVolunteered', 'isSweepBlack', 'isSend',
            'sendDepartment', 'progress', 'level', 'conclusion', 'fourShapes', 'processResult']
        for (let index = 0; index < selects.length; index++) {
            let name = selects[index]
            // console.log("dataObj[name]: ", dataObj[name])
            if (dataObj[name] == null) {
                dataObj[name] = -1
            }
        }
        console.log("dataObj: ", dataObj)
        return dataObj
    },
    /**
     * 返回的list Null转空字符
     */
    parseData: function (dataObj) {
        // console.log("dataObj: ", dataObj)
        for (let index = 0; index < dataObj.length; index++) {
            let item = dataObj[index]
            item = this.nullTransStr(item)
            item.involvedRegion = this.nullTransStr(item.involvedRegion)
            // item.reporteders = this.nullTransStr(item.reporteders)
            // item.reporter = this.nullTransStr(item.reporter)
            dataObj[index] = item
        }
        return dataObj
    },
    /**
     * code，name换名字，去除isDeleted
     */
    renameOption: function (dataObj) {
        var options = []
        for (let index = 0; index < dataObj.length; index++) {
            var option = {
                value: dataObj[index].code,
                label: dataObj[index].name
            }
            options.push(option)
        }
        return options
    },
    /**
     * 根据name反向查code
     */
    getCodeFromName: function (dataObj, name) {
        let code = null
        for (let index = 0; index < dataObj.length; index++) {
            if (dataObj[index].label == name) {
                code = dataObj[index].value
                break
            }
        }
        return code
    },
    addChildren: function (parents, child, ingoreValue) {
        for (let index = 0; index < parents.length; index++) {
            if (parents[index].value != ingoreValue) {
                parents[index].children = child
            }
        }
        parents = this.deleteUnselected(parents)
        return parents
    },
    deleteUnselected: function (dataObj) {
        var newObj = []
        for (let index = 0; index < dataObj.length; index++) {
            if (dataObj[index].value != -1) {
                newObj.push(dataObj[index])
            }
        }
        return newObj
    },
    /**
     * 获取级联选择器的data
     */
    getCascaderData: function (dataObj) {
        let title = this.deleteUnselected(dataObj.title)
        let blackDepartment = dataObj.blackDepartment
        blackDepartment = this.addChildren(blackDepartment, title, -1)
        let caseContentOne = this.addChildren(dataObj.caseContentOne, blackDepartment, -1)
        let caseContentTwo = this.addChildren(dataObj.caseContentTwo, blackDepartment, -1)
        let caseType = dataObj.caseType
        for (let index = 0; index < caseType.length; index++) {
            if (caseType[index].value == 0) {
                caseType[index].children = caseContentOne
            } else if (caseType[index].value == 1) {
                caseType[index].children = caseContentTwo
            } else {
                continue
            }
        }
        caseType = this.deleteUnselected(caseType)
        let isPolice = this.addChildren(dataObj.isPolice, caseType, 0)
        // console.log("getCascaderData: ", isPolice)
        return isPolice
    },
    /**
     * 根据后端结果获取级联选择器的value
     */
    getCascaderSelectedValue: function (dataObj) {
        let selected = [dataObj.isPolice]
        if (selected[0] != 0) {
            selected.push(dataObj.caseType)
            switch (selected[1]) {
                case 0:
                    selected.push(dataObj.caseContentOne)
                    selected.push(dataObj.blackDepartment)
                    selected.push(dataObj.title)
                    break;
                case 1:
                    selected.push(dataObj.caseContentTwo)
                    selected.push(dataObj.blackDepartment)
                    selected.push(dataObj.title)
                    break;
                case 2:
                    break;
            }
        }
        return selected
    },
    /**
     * 
     */
    watermark: function (settings) {
        var defaultSettings = {
            watermark_txt: settings,
            watermark_x: 50, //水印起始位置x轴坐标
            watermark_y: 50, //水印起始位置Y轴坐标
            watermark_rows: 6, //水印行数
            watermark_cols: 6, //水印列数
            watermark_x_space: 300, //水印x轴间隔
            watermark_y_space: 250, //水印y轴间隔
            watermark_color: '#000', //水印字体颜色
            watermark_alpha: 0.15, //水印透明度
            watermark_fontsize: '18px', //水印字体大小
            // watermark_font: '华文彩云', //水印字体
            watermark_font: '微软雅黑',
            watermark_width: 160, //水印宽度
            watermark_height: 100, //水印长度
            watermark_angle: 15 //水印倾斜度数
        };
        if (arguments.length === 1 && typeof arguments[0] === "object") {
            var src = arguments[0] || {};
            for (key in src) {
                if (src[key] && defaultSettings[key] && src[key] === defaultSettings[key]) continue;
                else if (src[key]) defaultSettings[key] = src[key];
            }
        }
        var oTemp = document.createDocumentFragment();
        //获取页面最大宽度
        // var page_width = Math.max(document.body.scrollWidth, document.body.clientWidth);
        var page_width = 1000;
        var cutWidth = page_width * 0.0150;
        var page_width = page_width - cutWidth;
        //获取页面最大高度
        var page_height = Math.max(document.body.scrollHeight, document.body.clientHeight); //+ 450;
        page_height = Math.max(page_height, window.innerHeight - 30);
        //如果将水印列数设置为0，或水印列数设置过大，超过页面最大宽度，则重新计算水印列数和水印x轴间隔
        if (defaultSettings.watermark_cols == 0 || (parseInt(defaultSettings.watermark_x + defaultSettings.watermark_width * defaultSettings.watermark_cols + defaultSettings.watermark_x_space * (defaultSettings.watermark_cols - 1)) > page_width)) {
            defaultSettings.watermark_cols = parseInt((page_width - defaultSettings.watermark_x + defaultSettings.watermark_x_space) / (defaultSettings.watermark_width + defaultSettings.watermark_x_space));
            defaultSettings.watermark_x_space = parseInt((page_width - defaultSettings.watermark_x - defaultSettings.watermark_width * defaultSettings.watermark_cols) / (defaultSettings.watermark_cols - 1));
        }
        //如果将水印行数设置为0，或水印行数设置过大，超过页面最大长度，则重新计算水印行数和水印y轴间隔
        if (defaultSettings.watermark_rows == 0 || (parseInt(defaultSettings.watermark_y + defaultSettings.watermark_height * defaultSettings.watermark_rows + defaultSettings.watermark_y_space * (defaultSettings.watermark_rows - 1)) > page_height)) {
            defaultSettings.watermark_rows = parseInt((defaultSettings.watermark_y_space + page_height - defaultSettings.watermark_y) / (defaultSettings.watermark_height + defaultSettings.watermark_y_space));
            defaultSettings.watermark_y_space = parseInt(((page_height - defaultSettings.watermark_y) - defaultSettings.watermark_height * defaultSettings.watermark_rows) / (defaultSettings.watermark_rows - 1));
        }
        var x;
        var y;
        for (var i = 0; i < defaultSettings.watermark_rows; i++) {
            y = defaultSettings.watermark_y + (defaultSettings.watermark_y_space + defaultSettings.watermark_height) * i;
            for (var j = 0; j < defaultSettings.watermark_cols; j++) {
                x = defaultSettings.watermark_x + (defaultSettings.watermark_width + defaultSettings.watermark_x_space) * j;
                var mask_div = document.createElement('div');
                mask_div.id = 'mask_div' + i + j;
                mask_div.className = 'mask_div';
                mask_div.appendChild(document.createTextNode(defaultSettings.watermark_txt));
                //设置水印div倾斜显示
                mask_div.style.webkitTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
                mask_div.style.MozTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
                mask_div.style.msTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
                mask_div.style.OTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
                mask_div.style.transform = "rotate(-" + defaultSettings.watermark_angle + "deg)";
                mask_div.style.visibility = "";
                mask_div.style.position = "absolute";
                mask_div.style.left = x + 'px';
                mask_div.style.top = y + 'px';
                mask_div.style.overflow = "hidden";
                mask_div.style.zIndex = "9999";
                //让水印不遮挡页面的点击事件
                mask_div.style.pointerEvents = 'none';
                mask_div.style.opacity = defaultSettings.watermark_alpha;
                mask_div.style.fontSize = defaultSettings.watermark_fontsize;
                mask_div.style.fontFamily = defaultSettings.watermark_font;
                mask_div.style.color = defaultSettings.watermark_color;
                mask_div.style.textAlign = "center";
                mask_div.style.width = defaultSettings.watermark_width + 'px';
                mask_div.style.height = defaultSettings.watermark_height + 'px';
                mask_div.style.display = "block";
                oTemp.appendChild(mask_div);
            };
        };
        var shuiyinbox = document.getElementById('con_min')
        shuiyinbox.appendChild(oTemp);
    }
};
